<?php
namespace app\admin\controller;

use think\controller;

class Banner extends Admin
{
    protected $model;

    public function _initialize()
    {
        $this->model = model('am_banner');
    }

    //轮播图展示页
    public function bannerList()
    {
        $res = $this->model->selectAll();
        $this->assign('res',$res);
        return view('bannerList');
    }

    //轮播图添加页面
    public function banAddPage()
    {
        $res = $this->model->selectAll();
        $this->assign('res',$res);
        return view('ban_add_page');
    }

    //轮播图添加
    public function banAdd()
    {
        $data['title'] = input('title');
        $data['link'] = input('link');
        $data['img'] = input('img');
        $data['img_thumb'] = input('img_thumb');
        $data['sort'] = input('sort');
        $data['status'] = input('status');
        $data['position'] = input('position');
        $data['create_at'] = time();
        $result = $this->model->insertBanner($data);
        if($result>0){
            echo "<script>alert('添加成功');window.location.href = 'banlist'</script>";
        }else{
            echo "<script>alert('添加失败');window.history.go(-1)</script>";
        }
    }

    //轮播图修改页面
    public function banUpdatePage()
    {
        $id = input('id');
        $result = $this->model->updateBanner($id);
        $this->assign('result',$result);
        return view('ban_update_page');
    }

    //轮播图修改
    public function banUpdate()
    {
        $id = input('id');
        $data['title'] = input('title');
        $data['link'] = input('link');
        $data['img'] = input('img');
        $data['img_thumb'] = input('img_thumb');
        $data['sort'] = input('sort');
        $data['status'] = input('status');
        $data['position'] = input('position');
        $result = $this->model->editBanner($data,$id);
        if($result>0){
            echo "<script>alert('修改成功');window.location.href = 'banupdatepage'</script>";
        }else{
            echo "<script>alert('修改失败');window.history.go(-1)</script>";
        }
    }

    //轮播图删除
    public function banDelete()
    {
        $id = input('id');
        $res = $this->model->deleteBanner($id);
        if($res>0){
            echo "<script>alert('删除成功');window.location.href = 'banlist'</script>";
        }
    }
}